home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8974 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: lerc.nasa.gov!purdue!yuma!steffend
  2. From: steffend@lamar.colostate.edu (Dave Steffen)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Yet a question about random
  5. Date: 27 Feb 1996 17:55:48 GMT
  6. Organization: Colorado State University, Fort Collins, CO  80523
  7. Distribution: world
  8. Message-ID: <4gvgj4$4avq@yuma.ACNS.ColoState.EDU>
  9. References: <4gplos$lds@cc.tut.fi>
  10. NNTP-Posting-Host: glitch.physics.colostate.edu
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. Edvard Majakari (m151843@proffa.cc.tut.fi) wrote:
  14. > I use the following function to produce (pseudo)random numbers
  15. > between 1...top. However, this algorithm seems to be far from
  16. > good pseudorandom. But the hint was in rand manpage - any good
  17. > ideas to produce better 'random' numbers?
  18.  
  19. > -----&<------------------------&<-------------------------------
  20.  
  21. > #include <stdlib.h>
  22. > #include <sys/time.h>
  23. > #include <unistd.h>
  24.  
  25. > int randomize(float top) {
  26. >     int rnum;
  27. >     timeval seed;  //see struct timeval for further details
  28. >     gettimeofday(&seed, NULL);
  29. >     srand(seed.tv_usec); //initialize with millisecs
  30. >     rnum = 1+(int) (top*rand()/(RAND_MAX+1.0));
  31. >     return rnum;
  32. > }
  33.  
  34. > -----&<------------------------&<-------------------------------
  35. > --
  36. > //Ed            http://lodge.ton.tut.fi/~ed/
  37.  
  38.  
  39.     Check out "Numerical Recipes in C" 2nd Ed for several better
  40. random number generators; there are lots of other good references but
  41. I don't know about them personally. In general, the "system-supplied"
  42. random generator should not be trusted.
  43.                                  /\
  44.                                  \/
  45.  
  46. Dave Steffen                      No, his mind is not for rent
  47. Dept. of Physics                  To any God or Government
  48. Colorado State University         Always hopeful, yet discontent
  49. steffend@lamar.colostate edu      He knows changes aren't permanent-
  50.                       But change is...
  51. "Speak softly...                    
  52. ... and carry a black belt!"              -Neal Peart / RUSH
  53. -----------------------------------------------------------------------
  54.